package com.hy.config.note;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Component;

@Component
public class PhoneConfig {
    private static final String accessKeyId = "LTAI4G5Z7SvBrPBHEtNijSc4"; //TODO: 这里要写成你自己用户生成的
    private static final String accessKeySecret = "uPaBAkzHcA3nSRtHhcHWs2eq2LeUSd";//TODO: 这里要写成你自己生成的

    public boolean send(String phoneName, String templateCode, String code) {
        //<accessKeyId>用户的账号
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);  //使用阿里云的方法
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        //自定义的参数(手机号,签名,模板,验证码)
        request.putQueryParameter("PhoneNumbers", phoneName);//需要接受短信的手机号
        request.putQueryParameter("SignName", "花奕商城");//自己的签名
        request.putQueryParameter("TemplateCode", templateCode); //自己的模板
        request.putQueryParameter("TemplateParam", code);
        try {
            CommonResponse response = client.getCommonResponse(request);
            //默认有个验证成功
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;  //验证失败
    }
}
